import {
  ErrorRequestHandler,
  Request,
  Response,
  NextFunction as NF,
  response,
  NextFunction,
} from "express";
import { tokenTime } from "../../config";
import UserSer from "../../services/UserSer";
import { userinfo, type asyncPromise } from "../../interface";
import { info, err } from "../middleware/infoerr";
import ImgSer from "../../services/ImgSer";
export default {
  async login(req: Request, res: Response): asyncPromise {
    const data = await UserSer.login(req.body as userinfo);
    if (data instanceof Object) {
      res.cookie(
        "feifan",
        {
          name: data.name,
          id: data.id,
          imgUrl: data.imgUrl,
          right: data.right,
        },
        { maxAge: tokenTime * 1000, httpOnly: false, signed: true }
      );
      return info(res, "登录成功", data);
    }
    if (!data) return err(res, "账号或密码错误");
  },
  async registry(req: Request, res: Response): asyncPromise {
    const data = await UserSer.registry(req.body as userinfo);
    if (typeof data === "string") return err(res, data);
    if (data instanceof Object) {
      res.cookie(
        "feifan",
        {
          name: data.name,
          id: data.id,
          imgUrl: "/img/空.png",
          right: data.right,
        },
        { maxAge: tokenTime * 1000, httpOnly: false, signed: true }
      );
      return info(res, "注册成功", data);
    }
  },
  async isname(req: Request, res: Response) {
    if (typeof req.query.name === "string") {
      const is = await UserSer.isname(req.query.name);
      return info(res, is);
    }
  },
  async userimg(req: Request, res: Response) {
    req.query.fileurl = (req.query.fileurl as string).replace(/img/g, "upload");
    console.log(
      req.signedCookies.feifan.name,
      "上传了:",
      req.file?.originalname,
      "位置:",
      req.query.fileurl
    );
    const data = await UserSer.userupdate(
      {
        imgUrl: req.query.fileurl as string,
      },
      req.signedCookies.feifan.id
    );
    info(res, "修改成功", { url: req.query.fileurl, status: data });
  },

  async articleimg(req: Request, res: Response) {
    req.query.fileurl = (req.query.fileurl as string).replace(/img/g, "upload");
    console.log(
      req.signedCookies.feifan.name,
      "上传了:",
      req.file?.originalname,
      "位置:",
      req.query.fileurl
    );
    info(res, "修改成功", { url: req.query.fileurl });
  },
  async theftarticleimg(req: Request, res: Response, next: NextFunction) {
    try {
      req.query.fileurl = (req.query.fileurl as string).replace(
        /img/g,
        "upload"
      );
      console.log(
        req.signedCookies.feifan.name,
        "上传了:",
        req.file?.originalname,
        "位置:",
        req.query.fileurl
      );
      res.cookie(
        "feifan",
        {
          name: req.signedCookies.feifan.name,
          id: req.signedCookies.feifan.id,
          imgUrl: req.query.fileurl,
        },
        { maxAge: tokenTime * 1000, httpOnly: false, signed: true }
      );
      await ImgSer.insert(req.query.fileurl as string);
      info(res, "加密成功", { url: req.query.fileurl });
    } catch (error) {
      next(error);
    }
  },
  async getuserlist(req: Request, res: Response, next: NextFunction) {
    try {
      const result = await UserSer.getuserlist();
      return info(res, "ok", result);
    } catch (error) {
      next(error);
    }
  },
  async changeright(req: Request, res: Response, next: NextFunction) {
    try {
      console.log(req.body.id, req.body.right);

      if (
        req.body.id &&
        !Object.is(+req.body.id, NaN) &&
        (req.body.right == "2" || req.body.right == "3")
      ) {
        const id = +req.body.id;
        const data = { right: String(req.body.right) };
        const result = await UserSer.changeright(id, data);
        if (!result) return err(res, "no");
        return info(res, "ok");
      } else return err(res, "参数有误", req.body);
    } catch (error) {
      next(error);
    }
  },
};
